import plotly
print(plotly.__version__) # version 1.9.x required
plotly.offline.init_notebook_mode() # run at the start of every notebook
plotly.offline.iplot({
"data": [{
"x": [1, 2, 3],
"y": [4, 2, 5]
}],
"layout": {
"title": "hello world"
}
})
# (*) How you communicate with Plotly's servers
import plotly.plotly as py
# (*) Useful tools, e.g., get_sublots(), embed()
import plotly.tools as tls
# Every function in this module will communicate with an external plotly server
py.iplot({ # use `py.iplot` inside the ipython notebook
"data": [{
"x": [1, 2, 3],
"y": [4, 2, 5]
}],
"layout": {
"title": "hello world"
}
}, filename='hello world', # name of the file as saved in your plotly account
privacy='public')
# (1) Two lists of numbers
x1 = [1, 2, 3, 5, 6]
y1 = [1, 4.5, 7, 24, 38]
# (2) Make dictionary linking x and y coordinate lists to 'x' and 'y' keys
trace1 = dict(x=x1, y=y1)
# (3) Make list of 1 trace, to be sent to Plotly
data = [trace1]
py.plot(data, filename='s0_first_plot', auto_open=False)
# (@) Sent data to Plotly and show result in notebook
py.iplot(data, filename='s0_first_plot')
# (*) Graph objects to piece together plots
from plotly.graph_objs import Data, Layout, Figure
# (*) Import the Scatter graph object
from plotly.graph_objs import Scatter
# Make three lists of numbers
x = [1, 2, 3, 5, 6]
y1 = [1, 4.5, 7, 24, 38]
y2 = [1, 4, 9, 25, 36]
# (1.1) Make a 1st Scatter object
trace1 = Scatter(
x=x, # x-coordinates of trace
y=y1, # y-coordinates of trace
mode='markers' # scatter mode (more in UG section 1)
)
# (1.2) Make a 2nd Scatter object
trace2 = Scatter(
x=x, # same x-coordinates
y=y2, # different y-coordinates
mode='lines' # different scatter mode
)
# (2) Make Data object
data = Data([trace1, trace2]) # (!) Data is list-like, must use [ ]
# (3) Make Layout object (Layout is dict-like)
layout = Layout(title='Fig 0.3: Some Experiment')
# (4) Make Figure object (Figure is dict-like)
fig = Figure(data=data, layout=layout)
# (*) Import graph objects XAxis and YAxis
from plotly.graph_objs import XAxis, YAxis
# (6.1) Make XAxis object, add title key
xaxis = XAxis(title='Some independent variable')
# (6.2) Make YAxis object, add title key
yaxis = YAxis(title='Some dependent variable')
# (7) Update 'layout' key in the Figure object
fig['layout'].update(
xaxis1=xaxis, # link XAxis object to 'xaxis1' (corresp. to first/only x-axis)
yaxis1=yaxis # similarly for 'yaxis1'
)
# (@) Send Figure object to Plotly and show plot in notebook
py.iplot(fig, filename='s0_second-plot')
# (*) To communicate with Plotly's server, sign in with credentials file
import plotly.plotly as py
# (*) Useful Python/Plotly tools
import plotly.tools as tls
# (*) Graph objects to piece together plots
from plotly.graph_objs import *
import numpy as np # (*) numpy for math functions and arrays
# (*) Graph objects to piece together plots
from plotly.graph_objs import *
import numpy as np # (*) numpy for math functions and arrays
stream_ids = tls.get_credentials_file()['stream_ids']
# Get stream id from stream id list
stream_id = stream_ids[0]
print(stream_id)
# Make instance of stream id object
stream = Stream(
token=stream_id, # (!) link stream id to 'token' key
maxpoints=80 # (!) keep a max of 80 pts on screen
)
# Initialize trace of streaming plot by embedding the unique stream_id
trace1 = Scatter(
x=[],
y=[],
mode='lines+markers',
stream=stream # (!) embed stream id, 1 per trace
)
data = Data([trace1])
# Add title to layout object
layout = Layout(title='Time Series')
# Make a figure object
fig = Figure(data=data, layout=layout)
# (@) Send fig to Plotly, initialize streaming plot, open new tab
unique_url = py.plot(fig, filename='s7_first-stream', auto_open=False)
# (@) Make instance of the Stream link object,
# with same stream id as Stream id object
s = py.Stream(stream_id)
# (@) Open the stream
s.open()
# (*) Import module keep track and format current time
import datetime
import time
i = 0 # a counter
k = 5 # some shape parameter
N = 200 # number of points to be plotted
# Delay start of stream by 5 sec (time to switch tabs)
time.sleep(5)
while i<N:
i += 1 # add to counter
# Current time on x-axis, random numbers on y-axis
x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
y = (np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1))[0]
# (-) Both x and y are numbers (i.e. not lists nor arrays)
# (@) write to Plotly stream!
s.write(dict(x=x, y=y))
# (!) Write numbers to stream to append current data on plot,
# write lists to overwrite existing data on plot (more in 7.2).
time.sleep(0.08) # (!) plot a point every 80 ms, for smoother plotting
# (@) Close the stream when done plotting
s.close()
tls.embed('https://plot.ly/~ChristieMyburgh/28/time-series/')